package com.lession.service;

import com.lession.Dto.LoginVerificateReqDto;
import com.lession.Dto.LoginVerificateRspDto;
import com.lession.common.LessionException;
import com.lession.common.RetEnum;
import com.lession.mapper.UserMapper;
import com.lession.pojo.USER;
import com.lession.util.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private UserMapper userMapper;
	
	public LoginVerificateRspDto verificate(LoginVerificateReqDto param) {
		String userId = param.getUserId();
		String passWord = param.getPassword();
		if (StringUtils.isBlank(userId) || StringUtils.isBlank(passWord)) {
			logger.error("账号密码不能为空");
			throw new LessionException(RetEnum.DATA_IS_EMPTY_ERROR.getCode());
		}
		USER user = userMapper.login(userId);
		if (user == null) {
			logger.error("根据工号{}查询结果为空", userId);
			throw new LessionException(RetEnum.DATA_NOT_EXIST_ERROR.getCode());
		}
		if (!MD5Utils.getMD5Code(passWord).equals(user.getYgmm())) {
			logger.error("密码错误");
			throw new LessionException(RetEnum.LOGIN_AUTH_1003.getCode());
		}
		LoginVerificateRspDto dto = new LoginVerificateRspDto();
		dto.setUserId(userId);
		dto.setUserName(user.getYgmc());
		return dto;
	}
}
